﻿using K8.CBB;
using System.IO;

namespace KingerRobot.UnitTest;
[TestClass]
public sealed class Program
{
    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context)
    {
        ini_main();
    }

    static void ini_main()
    {
        void ini_db_config_sql()
        {
            var dbConfig = new K8.CBB.DbConfig
            {
                DbAlias = "MsSql",
                DbType = "MsSql",
                DbHost = "127.0.0.1",
                DbUser = "sa",
                DbPwd = "123456",
                DbName = "K8_WCS_GSQ"
            };
            XApp.Current.DbConfig = dbConfig.ToJson();
        }
        void ini_db_config_redis()
        {
            var dbConfig = new DbConfig
            {
                DbAlias = "K8.Redis",
                DbType = "Redis",
                DbHost = "127.0.0.1",
                DbPort = 6379,
                DbUser = string.Empty,
                DbPwd = "OGsFD3QLh5LT",
                DbName = "14"
            };
            XApp.Current.RedisDB = dbConfig.ToJson();
        }

        void ini_run_app_main()
        {
            try
            {
                // 指定要搜索的根目录
                string rootDirectory = @"D:\K8";

                // 获取所有以“x-runtimes”开头的文件夹
                var runtimeDirectories = Directory.GetDirectories(rootDirectory, "x-runtimes*", SearchOption.AllDirectories);

                foreach (var directory in runtimeDirectories)
                {
                    // 获取目录中的所有 DLL 文件
                    var dllFiles = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories);

                    foreach (var dll in dllFiles)
                    {
                        try
                        {
                            // 加载 DLL 程序集
                            Assembly.LoadFrom(dll);
                            Console.WriteLine($"Successfully loaded: {dll}");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Failed to load {dll}: {ex.Message}");
                        }
                    }
                }


                //var log_action = new Action<string>((msg) =>
                //{
                //    return;
                //});
                //var typeName = $"K8.App.Program,K8.App";
                //var type = Type.GetType(typeName, false, true);
                //var method = type.GetMethod("Main", TypeExtend.DefaultBindingFlags(), [typeof(string[])]);
                //if (method != null)
                //{
                //    method.Invoke(null, new object[method.GetParameters().Length]);
                //    return;
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        void ini_db_config()
        {
            ini_db_config_sql();
            ini_db_config_redis();
        }

        ini_run_app_main();
        ini_db_config();
    }
}
